/*
 * author : Dinh Ngoc Tuan Vu (A0092633)
 */
package sg.edu.nus.iss.team4ft.service;

import java.io.Serializable;
import java.util.Date;

import sg.edu.nus.iss.team4ft.util.DateUtil;

public class Feature extends ServiceSubscription  implements Serializable {

	private static final long serialVersionUID = 1L;
	private FeatureCode featureCode;
	private Double chargePerUnit;
	private Plan plan;
	private boolean isDefaultService = false;
	
	protected Feature(Feature feature) throws CloneNotSupportedException{
		super((ServiceSubscription)feature);
		this.featureCode = feature.featureCode;
		this.chargePerUnit = feature.chargePerUnit;
		if(feature.plan != null )
			this.plan = feature.plan.clone();
		isDefaultService = feature.isDefaultService;
	}
	
	public Feature (Date startDate, FeatureCode featureCode){
		super(startDate);
		this.featureCode = featureCode;
	}
	
	public void setFeatureCode(FeatureCode featureCode) {
		this.featureCode = featureCode;
	}
	
	public FeatureCode getFeatureCode() {
		return this.featureCode;
	}
	
	public void setChargePerUnit(Double chargePerUnit) {
		this.chargePerUnit = chargePerUnit;
	}
	
	public Double getChargePerUnit() {
		return chargePerUnit;
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}
	
	//A new Feature can be added only if
	//	Condition 1. No such Feature exist (or)
	//	Condition 2. A Feature of same type exist but it is unsubscripted
	@Override
	public boolean canInclude(ServiceSubscription service) {
		boolean canInclude = super.canInclude(service);
		
		if(service instanceof Feature)
		{
			Feature f = (Feature)service;
			//check condition 2 stated above
			if(this.featureCode == f.featureCode && this.isSubscriptionActive(DateUtil.getDate()) && f.getEndDate() == null)
			{
				canInclude = false;
			}
		}
		return canInclude;
	}
	
	public boolean equals(FeatureCode featureCode){
		if (this.featureCode == featureCode){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean equals(Feature feature){
		if (this.featureCode == feature.featureCode){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	@Override
	public Feature clone() throws CloneNotSupportedException {
//		Feature f = new Feature(this.getStartDate(), this.featureCode);
//		f.setChargePerUnit(this.getChargePerUnit());
//		try {
//			f.setEndDate(this.getEndDate());
//		} catch (InvalidOperationException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		f.setFeatureCode(this.getFeatureCode());
//		f.setPlan(this.getPlan());
//		f.setSubscriptionCharge(this.getSubscriptionCharge());
		return new Feature(this);
	}

	public boolean getIsDefaultService() {
		return isDefaultService;
	}

	public void setIsDefaultService(boolean isDefaultService) {
		this.isDefaultService = isDefaultService;
	}
}

